//
//  MHWebViewViewController.m
//  MoRan
//
//  Created by MoRan on 2022.
//  Copyright © 2022 MoRan. All rights reserved.
//

#import "BaseWebViewController.h"
#import <WebKit/WebKit.h>

@interface BaseWebViewController ()

//webview,加载网页用。
@property (strong, nonatomic) WKWebView *webView;

@end

@implementation BaseWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationBar.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavigationItemTitleViewWithTitle:kSafeString(_webTitle)];
        
    WKWebViewConfiguration *webConfiguration = [WKWebViewConfiguration new];
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, self.navigationBarHeight, kScreenWidth, kScreenHeight - self.navigationBarHeight) configuration:webConfiguration];
    _webView.navigationDelegate = self;
    [self.view addSubview:_webView];
    [self.view sendSubviewToBack:_webView];
    _webView.backgroundColor = [UIColor clearColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"延迟秒后打印出来的日志！");
        [SVProgressHUD show];
        NSString *urlStr = _url;
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    });
}

- (void)addBackImageView {
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)showBack {
    NSString *imageName = @"black_back";
    [self setLeftItemWithIcon:[UIImage imageNamed:imageName] selector:@selector(backAction:)];
}

- (void)backAction:(UIButton *)sender {
    if (self.webView.canGoBack) {
        [self.webView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

/* 页面加载完成 */
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    [SVProgressHUD dismiss];
}

/* 页面加载失败 */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation {
    [SVProgressHUD dismiss];
}

@end
